package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.domain.operatelog.OperateLogDO;
import com.qk.dto.log.OperateLogDTO;
import com.qk.entity.OperateLog;
import com.qk.entity.User;
import com.qk.management.mapper.OperateLogMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.OperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Just、倾城
 * @created 2025-10-17
 * @description
 *
 */
@Service
@Slf4j
public class OperateLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLog> implements OperateLogService {
    @Autowired
    private UserMapper userServiceMapper;
    @Override
    public PageResult<OperateLogDO> pageList(OperateLogDTO dto) {

        Page<OperateLog> operateLogPage = new Page<>(dto.getPage(), dto.getPagesize());
        User user = userServiceMapper.selectByUsername(dto.getOperateUserName());
        Page<OperateLog> myPageResult = null;
        if (!ObjectUtil.isEmpty(user)) {
            myPageResult = this.baseMapper.selectPage(operateLogPage, Wrappers.lambdaQuery(OperateLog.class)
                    .eq(OperateLog::getOperateUserId, user.getId()));
        }else {
            myPageResult = new Page<>();
        }
        List<OperateLog> toList = myPageResult.getRecords();
        List<OperateLogDO> resultList = toList.stream().map(


                item -> {
                    OperateLogDO operateLogDO = BeanUtil.copyProperties(item, OperateLogDO.class);
                    operateLogDO.setOperateUserName(user.getName());
                    return operateLogDO;
                }


        ).toList();
        return PageResult.<OperateLogDO>builder()
                .total(Long.valueOf(myPageResult.getTotal()).intValue())
                .rows(resultList)
                .build();
    }
}
